
const {ccclass, property} = cc._decorator;

@ccclass
export default class Hero2 extends cc.Component {

    // LIFE-CYCLE CALLBACKS:
    // 键盘按下或抬起
    private Input:{} = { };
    // 人物方向
    sp:cc.Vec2 = cc.v2(0, 0);
    // 给物理引擎设置方向
    lv:cc.Vec2 = cc.v2(0, 0);
    // 人物速度
    @property(cc.Integer)
    speed:number = 0;

    // 是否正在跳跃
    isJumping:boolean = false;
    @property
    jumpHeight:number =  0;
    // 主角跳跃持续时间
    @property
    jumpDuration:number = 0;

    jumpAction:any // 跳跃动作

    // 动画
    private curAni:string = '';
    aniHero:cc.Animation

    onLoad () {
        // 初始化键盘输入监听
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this); 
        this.node.getComponent(cc.RigidBody).enabledContactListener = true; // 开启碰撞监听

        // 动画
        this.aniHero = this.node.getComponent(cc.Animation);
        this.setAni('person');
    }

    // 键盘按下事件
    onKeyDown(event: cc.Event.EventKeyboard) {
        this.Input[event.keyCode] = 1;
        let keyCode = event.keyCode;

        this.moveDirect(event.keyCode);
    }
    // 键盘抬起事件
    onKeyUp(event: cc.Event.EventKeyboard) {
        this.Input[event.keyCode] = 0;

        this.sp.x = 0;
        this.setAni('person');
    }

    // moveDirect 移动判断
    moveDirect(keyCode:number) { 
        switch (keyCode) {
            case cc.macro.KEY.a:
            case cc.macro.KEY.left:
                // 向左
                this.sp.x = -1;
                this.node.scaleX = -1;
                this.setAni('personRun');
                break;
            case cc.macro.KEY.d:
            case cc.macro.KEY.right:
                // 向右
                this.sp.x = 1;
                this.node.scaleX = 1;
                this.setAni('personRun');
                break;
            case cc.macro.KEY.w:
            case cc.macro.KEY.up:
                // 向上
                if (this.isJumping) return;
                this.runJumpAction();
                this.setAni('person');
                break;
        }
    }

    // 跳跃方法
    runJumpAction() {
        this.isJumping = true;
        var finished = cc.callFunc((target)=>{
            // console.log('动作结束');
            // 唤醒刚体
            this.node.getComponent(cc.RigidBody).awake = true;
        }, this);
        // 跳跃上升
        this.jumpAction = cc.sequence(cc.moveBy(this.jumpDuration, 0, this.jumpHeight), finished);
        this.node.runAction(this.jumpAction)
    }

    // 碰撞监听
    onBeginContact(contact:any, selfCollider:any, otherCollider:any) {
        this.isJumping = false; 
        this.node.stopAction(this.jumpAction)
        
        if (otherCollider.node.group == 'game2_trap') {
            // 如果撞到的是陷阱
            // console.log(otherCollider.node.group);
            this.personDie();
        }
        // 
        
    }
    // 人物死亡
    personDie() {
        // console.log('人物死亡');
        
    }

    // 设置人物动画
    setAni(anima:string) {
        if (this.curAni == anima) return;
        this.curAni = anima;
        this.aniHero.play(this.curAni);
    }

    update (dt) {
        // 添加物理碰撞引擎后，不能通过改变坐标来使人物移动，需要通过改变线性速度
        this.lv = this.node.getComponent(cc.RigidBody).linearVelocity;

        if (this.sp.x != 0) {
            this.lv.x = this.sp.x * this.speed;
            // console.log(this.lv.x);
            // this.node.x += this.sp.x * this.speed * dt;
        } else {
            this.lv.x = 0;
        }

        this.node.getComponent(cc.RigidBody).linearVelocity = this.lv;
    }
}
